import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:inmotion_pro/config/r.dart';
import 'package:inmotion_pro/l10n/local.dart';
import 'package:inmotion_pro/pages/models/vehicle_function_button_model.dart';
import 'package:inmotion_pro/pages/others/common_enum/riding_mode_type.dart';
import 'package:inmotion_pro/pages/main/vehicle_setting/helper.dart';
import 'package:inmotion_pro/utils/utils.dart';

import '../others/common_enum/cell_setting_type.dart';
import '../others/common_enum/vehicle_function_button_type.dart';
import '../others/common_enum/vehicle_light_type.dart';
import '../others/custom_view/setting_items/setting_leading.dart';
import '../models/settting_item_model.dart';

// 车辆类型混合类
mixin VehicleTypeMixin {
  // MARK: - 设备名称是否有效
  bool vehicleTypeMixinIsValidVehicleName(
      {String? bleName, String? productModel}) {
    return bleName != null &&
        vehicleTypeMixinIsValidLorinVehicleName(
            bleName: bleName, productModel: productModel);
  }

  // MARK: - Lorin名称是否有效
  bool vehicleTypeMixinIsValidLorinVehicleName(
      {String? bleName, String? productModel}) {
    final RegExp lorinRegExp = RegExp(r'^[a-zA-Z][0-9]{1,2}.*-.*$');
    return bleName != null &&
        (lorinRegExp.hasMatch(bleName) ||
            vehicleTypeMixinIsRsJet(
                bleName: bleName, productModel: productModel));
  }

  // MARK: - V9
  bool vehicleTypeMixinIsV9({String? bleName, String? productModel}) {
    return bleName != null && (bleName.toLowerCase().startsWith('v9-'));
  }

  // MARK: - V12s
  bool vehicleTypeMixinIsV12s({String? bleName, String? productModel}) {
    return bleName != null &&
        (bleName.toLowerCase().startsWith('v12s-') ||
            bleName.toLowerCase().startsWith('v18-'));
  }

  // MARK: - P6
  bool vehicleTypeMixinIsP6({String? bleName, String? productModel}) {
    return bleName != null && (bleName.toLowerCase().startsWith('p6-'));
  }

  // MARK: - Rs_Jet
  bool vehicleTypeMixinIsRsJet({String? bleName, String? productModel}) {
    return bleName != null &&
        (bleName.toLowerCase().startsWith('rs_jet-') ||
            bleName.toLowerCase().startsWith('rs_velo-'));
  }

  // MARK: - 是否可以使用综合产品信息和综合实时信息
  bool vehicleTypeMixinIsSupportIntegratedInfo(String hmicVersion,
      {String? bleName, String? productModel}) {
    return bleName != null &&
            ((vehicleTypeMixinIsV9(
                        bleName: bleName, productModel: productModel) &&
                    Utils.compareVersions(hmicVersion, "1.8.50") >= 0) ||
                (vehicleTypeMixinIsV12s(
                        bleName: bleName, productModel: productModel) &&
                    Utils.compareVersions(hmicVersion, "1.8.68") >= 0)) ||
        (vehicleTypeMixinIsP6(bleName: bleName, productModel: productModel) &&
            Utils.compareVersions(hmicVersion, "1.8.68") >= 0);
  }

  // MARK: - 是否支持实时信息主动上报
  bool vehicleTypeMixinIsSupportRealtimePush(String hmicVersion,
      {String? bleName, String? productModel}) {
    return bleName != null &&
        (vehicleTypeMixinIsP6(bleName: bleName, productModel: productModel) &&
            Utils.compareVersions(hmicVersion, "1.9.90") >= 0);
  }

  // MARK: - 根据蓝牙名称或产品型号获取车辆类型
  VehicleType vehicleTypeMixinGetVehicleType(
      {String? bleName, String? productModel}) {
    if (bleName != null) {
      if (vehicleTypeMixinIsV9(bleName: bleName, productModel: productModel)) {
        return VehicleType.v9;
      } else if (vehicleTypeMixinIsV12s(
          bleName: bleName, productModel: productModel)) {
        return VehicleType.v12s;
      } else if (vehicleTypeMixinIsRsJet(
          bleName: bleName, productModel: productModel)) {
        return VehicleType.rsJet;
      } else if (vehicleTypeMixinIsP6(
          bleName: bleName, productModel: productModel)) {
        return VehicleType.p6;
      }
    }
    return VehicleType.unknown;
  }
}

// Vehicle Type Enum
enum VehicleType {
  unknown,
  v9,
  v12s,
  p6,
  rsJet;

  // MARK: - LOGO图片路径
  String get assetPath {
    switch (this) {
      case VehicleType.v9:
        return R.v9;
      case VehicleType.v12s:
        return R.v12s;
      case VehicleType.rsJet:
        return R.rsJet;
      case VehicleType.p6:
        return R.p6;
      case VehicleType.unknown:
        return R.failedImageIcon;
    }
  }

  // MARK: - 灰色LOGO图片路径
  String get greyAssetPath {
    switch (this) {
      case VehicleType.v9:
        return R.v9Grey;
      case VehicleType.v12s:
        return R.v12sGrey;
      case VehicleType.rsJet:
        return R.rsJetGrey;
      case VehicleType.p6:
        return R.p6Grey;
      case VehicleType.unknown:
        return R.failedImageIcon;
    }
  }

  // MARK: - 获取低电压临界点
  double getLowVoltageThreshold() {
    switch (this) {
      case VehicleType.v9:
        return 12.0;
      case VehicleType.v12s:
        return 12.0;
      case VehicleType.rsJet:
        return 12.0;
      case VehicleType.p6:
        return 12.0;
      case VehicleType.unknown:
        return 12.0;
    }
  }

  // MARK: - 获取高电压临界点
  double getHighVoltageThreshold() {
    switch (this) {
      case VehicleType.v9:
        return 1000.0;
      case VehicleType.v12s:
        return 1000.0;
      case VehicleType.rsJet:
        return 1000.0;
      case VehicleType.p6:
        return 1000.0;
      case VehicleType.unknown:
        return 1000.0;
    }
  }

  // MARK: - 获取输出矢量电压百分比低临界点
  double getOutputVectorVoltagePercentageLowThreshold() {
    switch (this) {
      case VehicleType.v9:
        return 0.0;
      case VehicleType.v12s:
        return 0.0;
      case VehicleType.rsJet:
        return 0.0;
      case VehicleType.p6:
        return 0.0;
      case VehicleType.unknown:
        return 0.0;
    }
  }

  // MARK: - 获取输出矢量电压百分比高临界点
  double getOutputVectorVoltagePercentageHighThreshold() {
    switch (this) {
      case VehicleType.v9:
        return 1000.0;
      case VehicleType.v12s:
        return 1000.0;
      case VehicleType.rsJet:
        return 1000.0;
      case VehicleType.p6:
        return 1000.0;
      case VehicleType.unknown:
        return 1000.0;
    }
  }

  // MARK: - 获取电机温度低临界点
  double getMotorTemperatureLowThreshold() {
    switch (this) {
      case VehicleType.v9:
        return 0.0;
      case VehicleType.v12s:
        return 0.0;
      case VehicleType.rsJet:
        return 0.0;
      case VehicleType.p6:
        return 0.0;
      case VehicleType.unknown:
        return 0.0;
    }
  }

  // MARK: - 获取电机温度高临界点
  double getMotorTemperatureHighThreshold() {
    switch (this) {
      case VehicleType.v9:
        return 100.0;
      case VehicleType.v12s:
        return 100.0;
      case VehicleType.rsJet:
        return 100.0;
      case VehicleType.p6:
        return 100.0;
      case VehicleType.unknown:
        return 100.0;
    }
  }

  // MARK: - 获取电池温度低临界点
  double getBatteryTemperatureLowThreshold() {
    switch (this) {
      case VehicleType.v9:
        return 0.0;
      case VehicleType.v12s:
        return 0.0;
      case VehicleType.rsJet:
        return 0.0;
      case VehicleType.p6:
        return 0.0;
      case VehicleType.unknown:
        return 0.0;
    }
  }

  // MARK: - 获取电池温度高临界点
  double getBatteryTemperatureHighThreshold() {
    switch (this) {
      case VehicleType.v9:
        return 100.0;
      case VehicleType.v12s:
        return 100.0;
      case VehicleType.rsJet:
        return 100.0;
      case VehicleType.p6:
        return 100.0;
      case VehicleType.unknown:
        return 100.0;
    }
  }

  // MARK: - 根据锁车状态、灯光状态获取车辆的功能按钮列表
  List<VehicleFunctionButtonModel> getVehicleFunctionButtonList({
    required bool isLocked,
    required VehicleLightType lightType,
  }) {
    List<VehicleFunctionButtonModel> buttonList = [];
    for (var button in _getFunctionButtons()) {
      switch (button) {
        case VehicleFunctionButtonType.horn:
          buttonList.add(VehicleFunctionButtonModel()
            ..type = VehicleFunctionButtonType.horn
            ..title = local.horn
            ..assetPath = R.hornIcon);
          break;
        case VehicleFunctionButtonType.lock:
          buttonList.add(VehicleFunctionButtonModel()
            ..type = VehicleFunctionButtonType.lock
            ..title = isLocked ? local.lock : local.unlock
            ..assetPath = isLocked ? R.lockIcon : R.unlockIcon);
          break;
        case VehicleFunctionButtonType.light:
          buttonList.add(VehicleFunctionButtonModel()
            ..type = VehicleFunctionButtonType.light
            ..title = lightType.title
            ..assetPath = lightType.assetPath);
          break;
        case VehicleFunctionButtonType.settings:
          buttonList.add(VehicleFunctionButtonModel()
            ..type = VehicleFunctionButtonType.settings
            ..title = local.settings
            ..assetPath = R.vehicleSetting);
          break;
        case VehicleFunctionButtonType.realtimeDetail:
          // 不处理
          break;
      }
    }
    return buttonList;
  }

  // MARK: - 获取功能按钮
  List<VehicleFunctionButtonType> _getFunctionButtons() {
    switch (this) {
      case VehicleType.v9:
        return [
          VehicleFunctionButtonType.horn,
          VehicleFunctionButtonType.lock,
          VehicleFunctionButtonType.light,
          VehicleFunctionButtonType.settings,
        ];
      case VehicleType.v12s:
        return [
          VehicleFunctionButtonType.horn,
          VehicleFunctionButtonType.lock,
          VehicleFunctionButtonType.light,
          VehicleFunctionButtonType.settings,
        ];
      case VehicleType.p6:
        return [
          VehicleFunctionButtonType.horn,
          VehicleFunctionButtonType.lock,
          VehicleFunctionButtonType.light,
          VehicleFunctionButtonType.settings,
        ];
      default:
        return [
          VehicleFunctionButtonType.horn,
          VehicleFunctionButtonType.lock,
          VehicleFunctionButtonType.settings,
        ];
    }
  }

  // MARK: - 根据前灯的值获取灯光类型
  VehicleLightType getLightType(int lampValue) {
    if (lampValue == 0) {
      return VehicleLightType.off;
    } else {
      return VehicleLightType.on;
    }
  }

  // MARK: - 根据lampValue，获取下一步操作的灯光值
  (bool, bool) getNextLightValue(VehicleLightType type) {
    bool isOff = type == VehicleLightType.off;
    return (isOff ? (true, true) : (false, false));
  }

  // MARK: - 获取骑行模式
  RidingModeType getRidingModeType(int mode) {
    switch (this) {
      case VehicleType.v9:
      case VehicleType.v12s:
      case VehicleType.rsJet:
      case VehicleType.p6:
        switch (mode) {
          case 0:
            return RidingModeType.normal;
          case 1:
            return RidingModeType.sport;
          default:
            return RidingModeType.unknown;
        }
      default:
        return RidingModeType.unknown;
    }
  }

  // MARK: - 骑行模式列表
  List<(RidingModeType, int)> getRidingModeList() {
    switch (this) {
      case VehicleType.v9:
      case VehicleType.v12s:
      case VehicleType.rsJet:
      case VehicleType.p6:
        return [
          (RidingModeType.normal, 0),
          (RidingModeType.sport, 1),
        ];
      default:
        return [];
    }
  }

  // MARK: - 获取允许设置的最大限速值, 单位 km/h
  int getMaxSpeedLimitByRidingMode(int mode) {
    switch (this) {
      case VehicleType.v9:
        return 4500;
      case VehicleType.v12s:
        switch (mode) {
          case 0:
            return 6000;
          case 1:
            return 7000;
          default:
            return 0;
        }
      case VehicleType.p6:
        switch (mode) {
          case 0:
            return 7000;
          case 1:
            return 15000;
          default:
            return 0;
        }
      default:
        return 0;
    }
  }

  // MARK: - 获取车辆设置items
  List<SettingSectionModel> getSettingItemList() {
    switch (this) {
      case VehicleType.v9:
      case VehicleType.v12s:
      case VehicleType.rsJet:
        final titles = [
          local.vehicle_nickname,
          local.vehicle_password,
          local.max_speed_limit,
          local.max_speed_warning,
          local.riding_mode,
          local.pedal_sensitivity,
          local.ride_assist,
          local.balance_angle,
          local.calibration,
          local.berm_angle_mode,
          local.spin_kill,
          local.sound_wave,
          local.sound,
          local.tbox_charge,
          local.tbox_reboot,
          local.stand_by_time,
          local.transport_mode,
          local.factory_reset,
        ];
        return _analyzeCellSectionType(titles);
      case VehicleType.p6:
        final titles = [
          local.vehicle_nickname,
          local.vehicle_password,
          local.max_speed_limit_section,
          local.max_speed_warning,
          local.riding_mode,
          local.pedal_sensitivity,
          local.ride_assist,
          local.balance_angle,
          local.calibration,
          local.berm_angle_mode,
          local.spin_kill,
          local.sound_wave,
          local.sound,
          local.tbox_charge,
          local.tbox_reboot,
          local.stand_by_time,
          local.transport_mode,
          local.factory_reset,
        ];
        return _analyzeCellSectionType(titles);
      case VehicleType.unknown:
        return [];
    }
  }

  // MARK: - 获取关于车辆items
  List<SettingSectionModel> getAboutVehicleCellList() {
    switch (this) {
      case VehicleType.v9:
      case VehicleType.v12s:
      case VehicleType.rsJet:
      case VehicleType.p6:
        final titles = [
          local.sn,
          local.bluetooth_name,
          local.model,
          local.activated_date,
          local.human_board,
          local.driver_board,
          local.bluetooth_board,
          local.BMS_1,
          local.BMS_2,
          local.display,
          local.tbox_set,
        ];
        return _analyzeCellSectionType(titles);
      case VehicleType.unknown:
        return [];
    }
  }

  // MARK: - 获取车辆更多功能items
  List<SettingSectionModel> getVehicleMoreFeaturesCellList() {
    switch (this) {
      case VehicleType.v9:
      case VehicleType.v12s:
      case VehicleType.rsJet:
      case VehicleType.p6:
        final titles = [
          local.battery_info,
          local.diagnosis,
          local.firmware_update,
          local.about_vehicle,
        ];
        return _analyzeCellSectionType(titles);
      case VehicleType.unknown:
        return [];
    }
  }

  // MARK: - 获取车辆BMS items
  List<SettingSectionModel> getVehicleBMSCellList() {
    switch (this) {
      case VehicleType.v9:
      case VehicleType.v12s:
      case VehicleType.rsJet:
      case VehicleType.p6:
        final titles = [
          local.bms_sn,
          local.bms_version,
          local.bms_hardware_version,
          local.voltage,
          local.bms_charging_status,
          local.bms_charging_current,
          local.bms_charging_mos,
          local.bms_charging_mos_temperature,
          local.bms_discharging_current,
          local.bms_discharging_mos,
          local.bms_discharging_mos_temperature,
          local.bms_fault_status,
          local.bms_warning_status,
          local.bms_protection_status,
          local.bms_cell_balance_status,
          local.bms_max_cell_voltage,
          local.bms_min_cell_voltage,
          local.bms_cell_voltage_difference,
          local.bms_max_cell_temperature,
          local.bms_min_cell_temperature,
          local.bms_cell_temperature_difference,
        ];
        return _analyzeCellSectionType(titles);
      case VehicleType.unknown:
        return [];
    }
  }

  // MARK: - 获取MinePage items
  List<SettingSectionModel> minePageCellList() {
    final titles = [
      local.ticket_service,
      local.ticket_history,
      local.product_manual,
      local.video_guide,
      local.online_customer_service,
      local.settings,
    ];
    return _analyzeCellSectionType(titles);
  }

  // MARK: - 获取用户信息 items
  List<SettingSectionModel> userInfoCellList() {
    final titles = [
      local.account,
      local.nick_name,
      local.signature,
    ];
    return _analyzeCellSectionType(titles);
  }

  // MARK: - 获取MineSetting items
  List<SettingSectionModel> mineSettingCellList() {
    final titles = [
      local.unit_metric_imperial,
      local.language,
      local.account_security,
      local.clear_cache,
      local.app_about,
    ];
    return _analyzeCellSectionType(titles);
  }

  // MARK: - 获取关于App的items
  List<SettingSectionModel> aboutAppCellList() {
    final titles = [
      local.user_agreement,
      local.privacy_policy,
    ];
    return _analyzeCellSectionType(titles);
  }

  // MARK: - 账号与安全
  List<SettingSectionModel> accountSecurityCellList() {
    final titles = [
      local.update_password,
      local.phone_number,
      local.email_address,
      local.bind_social_account,
      local.remove_account,
    ];
    return _analyzeCellSectionType(titles);
  }

  // MARK: - 根据title的内容，分析出对应的CellSectionType
  List<SettingSectionModel> _analyzeCellSectionType(List<String> rowTitle) {
    // 优化说明：用 map 快速查找每个 title 对应的构造器，避免长链 if/else，提高可读性与性能。
    final List<SettingRowModel> noneItems = [];
    final List<SettingRowModel> dataAndStatusItems = [];
    final List<SettingRowModel> securityItems = [];
    final List<SettingRowModel> preferenceItems = [];
    final List<SettingRowModel> repairItems = [];
    final List<SettingRowModel> inquiryItems = [];
    final List<SettingRowModel> soundItems = [];
    final List<SettingRowModel> lightItems = [];
    final List<SettingRowModel> tboxItems = [];
    final List<SettingRowModel> otherItems = [];

    // 映射每个 title 到 (sectionType, SettingRowModel builder)
    final Map<String, MapEntry<CellSectionType, SettingRowModel>> mapping = {
      // 序列号
      local.sn: MapEntry(
        CellSectionType.none,
        SettingRowModel(title: local.sn, rowType: CellRowType.onlyValue),
      ),
      // 车辆昵称
      local.vehicle_nickname: MapEntry(
        CellSectionType.none,
        SettingRowModel(
            title: local.vehicle_nickname, rowType: CellRowType.arrowWithValue),
      ),
      // 蓝牙名称
      local.bluetooth_name: MapEntry(
        CellSectionType.none,
        SettingRowModel(
            title: local.bluetooth_name, rowType: CellRowType.onlyValue),
      ),
      // 型号
      local.model: MapEntry(
        CellSectionType.none,
        SettingRowModel(title: local.model, rowType: CellRowType.onlyValue),
      ),
      // 激活日期
      local.activated_date: MapEntry(
        CellSectionType.none,
        SettingRowModel(
            title: local.activated_date, rowType: CellRowType.onlyValue),
      ),
      // 人机版本
      local.human_board: MapEntry(
        CellSectionType.none,
        SettingRowModel(
            title: local.human_board, rowType: CellRowType.onlyValue),
      ),
      // 驱动板
      local.driver_board: MapEntry(
        CellSectionType.none,
        SettingRowModel(
            title: local.driver_board, rowType: CellRowType.onlyValue),
      ),
      // 蓝牙板
      local.bluetooth_board: MapEntry(
        CellSectionType.none,
        SettingRowModel(
            title: local.bluetooth_board, rowType: CellRowType.onlyValue),
      ),
      // BMS 1
      local.BMS_1: MapEntry(
        CellSectionType.none,
        SettingRowModel(title: local.BMS_1, rowType: CellRowType.onlyValue),
      ),
      // BMS 2
      local.BMS_2: MapEntry(
        CellSectionType.none,
        SettingRowModel(title: local.BMS_2, rowType: CellRowType.onlyValue),
      ),
      // 显示
      local.display: MapEntry(
        CellSectionType.none,
        SettingRowModel(title: local.display, rowType: CellRowType.onlyValue),
      ),
      // TBOX 设置
      local.tbox_set: MapEntry(
        CellSectionType.none,
        SettingRowModel(title: local.tbox_set, rowType: CellRowType.onlyValue),
      ),
      // 车辆密码
      local.vehicle_password: MapEntry(
        CellSectionType.none,
        SettingRowModel(
            title: local.vehicle_password, rowType: CellRowType.onlyArrow),
      ),
      // 电池信息 (含图标与跳转)
      local.battery_info: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          leading: const SettingLeading(imgPath: R.batteryItemIcon),
          title: local.battery_info,
          rowType: CellRowType.onlyArrow,
          onTap: () => SettingHelper.instance.batteryInfoOnTap(),
        ),
      ),
      // 关于车辆 (含图标与跳转)
      local.about_vehicle: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          leading: const SettingLeading(imgPath: R.aboutVehicleItemIcon),
          title: local.about_vehicle,
          rowType: CellRowType.onlyArrow,
          onTap: () => SettingHelper.instance.aboutVehicleOnTap(),
        ),
      ),
      // 诊断 (含图标与跳转)
      local.diagnosis: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          leading: const SettingLeading(imgPath: R.faultDetectionItemIcon),
          title: local.diagnosis,
          rowType: CellRowType.onlyArrow,
          onTap: () => SettingHelper.instance.vehicleFaultDetectionOnTap(),
        ),
      ),
      // 固件更新 (含图标与跳转)
      local.firmware_update: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          leading: const SettingLeading(imgPath: R.firmwareUpgradeItemIcon),
          title: local.firmware_update,
          rowType: CellRowType.onlyArrow,
          onTap: () => SettingHelper.instance.firmwareUpdateOnTap(),
        ),
      ),
      // BMS 序列号
      local.bms_sn: MapEntry(
        CellSectionType.none,
        SettingRowModel(title: local.bms_sn, rowType: CellRowType.onlyValue),
      ),
      // BMS 版本
      local.bms_version: MapEntry(
        CellSectionType.none,
        SettingRowModel(
            title: local.bms_version, rowType: CellRowType.onlyValue),
      ),
      // BMS 硬件版本
      local.bms_hardware_version: MapEntry(
        CellSectionType.none,
        SettingRowModel(
            title: local.bms_hardware_version, rowType: CellRowType.onlyValue),
      ),
      // “我的”设置
      local.settings: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.settings,
          rowType: CellRowType.onlyArrow,
          trailing: _arrowWithOpacity(),
        ),
      ),
      // 公英制
      local.unit_metric_imperial: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.unit_metric_imperial,
          rowType: CellRowType.arrowWithValue,
        ),
      ),
      // 语言
      local.language: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.language,
          rowType: CellRowType.arrowWithValue,
        ),
      ),
      // 账号与安全
      local.account_security: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.account_security,
          rowType: CellRowType.onlyArrow,
        ),
      ),
      // 清理缓存
      local.clear_cache: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.clear_cache,
          rowType: CellRowType.arrowWithValue,
        ),
      ),
      // 关于应用
      local.app_about: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.app_about,
          rowType: CellRowType.arrowWithValue,
        ),
      ),
      // 用户协议
      local.user_agreement: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.user_agreement,
          rowType: CellRowType.onlyArrow,
        ),
      ),
      // 隐私政策
      local.privacy_policy: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.privacy_policy,
          rowType: CellRowType.onlyArrow,
        ),
      ),
      // 邮箱地址
      local.email_address: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.email_address,
          rowType: CellRowType.arrowWithValue,
        ),
      ),
      // 重置密码
      local.update_password: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.update_password,
          rowType: CellRowType.onlyArrow,
        ),
      ),
      // 手机号码
      local.phone_number: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.phone_number,
          rowType: CellRowType.arrowWithValue,
        ),
      ),
      // 第三方账号绑定
      local.bind_social_account: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.bind_social_account,
          rowType: CellRowType.onlyArrow,
        ),
      ),
      // 注销账号
      local.remove_account: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.remove_account,
          rowType: CellRowType.onlyArrow,
        ),
      ),
      // 账号
      local.account: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.account,
          rowType: CellRowType.onlyValue,
        ),
      ),
      // 昵称
      local.nick_name: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.nick_name,
          rowType: CellRowType.arrowWithValue,
        ),
      ),
      // 个性签名
      local.signature: MapEntry(
        CellSectionType.none,
        SettingRowModel(
          title: local.signature,
          rowType: CellRowType.arrowWithValue,
        ),
      ),

      // MARK: 【维修&服务】 分组 ---
      // 工单服务
      local.ticket_service: MapEntry(
        CellSectionType.repair,
        SettingRowModel(
          title: local.ticket_service,
          rowType: CellRowType.onlyArrow,
          leading: const SettingLeading(imgPath: R.ticketServiceItemIcon),
          trailing: _arrowWithOpacity(),
        ),
      ),
      // 工单记录
      local.ticket_history: MapEntry(
        CellSectionType.repair,
        SettingRowModel(
          title: local.ticket_history,
          rowType: CellRowType.onlyArrow,
          leading: const SettingLeading(imgPath: R.ticketHistoryItemIcon),
          trailing: _arrowWithOpacity(),
        ),
      ),
      // 在线客服
      local.online_customer_service: MapEntry(
        CellSectionType.inquiry,
        SettingRowModel(
          title: local.online_customer_service,
          leading:
              const SettingLeading(imgPath: R.onlineCustomerServiceItemIcon),
          rowType: CellRowType.onlyArrow,
          trailing: _arrowWithOpacity(),
        ),
      ),
      // 产品手册
      local.product_manual: MapEntry(
        CellSectionType.inquiry,
        SettingRowModel(
          title: local.product_manual,
          leading: const SettingLeading(imgPath: R.productManualItemIcon),
          rowType: CellRowType.onlyArrow,
          trailing: _arrowWithOpacity(),
        ),
      ),
      // 视频指南
      local.video_guide: MapEntry(
        CellSectionType.inquiry,
        SettingRowModel(
          title: local.video_guide,
          leading: const SettingLeading(imgPath: R.videoGuideItemIcon),
          rowType: CellRowType.onlyArrow,
          trailing: _arrowWithOpacity(),
        ),
      ),

      // MARK: 【数据&状态】 分组 ---
      // 电压
      local.voltage: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(title: local.voltage, rowType: CellRowType.onlyValue),
      ),
      // BMS 充电状态
      local.bms_charging_status: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_charging_status, rowType: CellRowType.onlyValue),
      ),
      // BMS 充电电流
      local.bms_charging_current: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_charging_current, rowType: CellRowType.onlyValue),
      ),
      // BMS 充电 MOS
      local.bms_charging_mos: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_charging_mos, rowType: CellRowType.onlyValue),
      ),
      // BMS 充电 MOS 温度
      local.bms_charging_mos_temperature: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_charging_mos_temperature,
            rowType: CellRowType.onlyValue),
      ),
      // BMS 放电电流
      local.bms_discharging_current: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_discharging_current,
            rowType: CellRowType.onlyValue),
      ),
      // BMS 放电 MOS
      local.bms_discharging_mos: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_discharging_mos, rowType: CellRowType.onlyValue),
      ),
      // BMS 放电 MOS 温度
      local.bms_discharging_mos_temperature: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_discharging_mos_temperature,
            rowType: CellRowType.onlyValue),
      ),
      // BMS 故障状态
      local.bms_fault_status: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_fault_status, rowType: CellRowType.onlyValue),
      ),
      // BMS 警告状态
      local.bms_warning_status: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_warning_status, rowType: CellRowType.onlyValue),
      ),
      // BMS 单体电池均衡状态
      local.bms_cell_balance_status: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_cell_balance_status,
            rowType: CellRowType.onlyValue),
      ),
      // BMS 最大单体电压
      local.bms_max_cell_voltage: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_max_cell_voltage, rowType: CellRowType.onlyValue),
      ),
      // BMS 最小单体电压
      local.bms_min_cell_voltage: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_min_cell_voltage, rowType: CellRowType.onlyValue),
      ),
      // BMS 单体电压差
      local.bms_cell_voltage_difference: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_cell_voltage_difference,
            rowType: CellRowType.onlyValue),
      ),
      // BMS 最大单体温度
      local.bms_max_cell_temperature: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_max_cell_temperature,
            rowType: CellRowType.onlyValue),
      ),
      // BMS 最小单体温度
      local.bms_min_cell_temperature: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_min_cell_temperature,
            rowType: CellRowType.onlyValue),
      ),
      // BMS 单体温度差
      local.bms_cell_temperature_difference: MapEntry(
        CellSectionType.dataAndStatus,
        SettingRowModel(
            title: local.bms_cell_temperature_difference,
            rowType: CellRowType.onlyValue),
      ),

      // // MARK: 【安全】 分组 ---
      // 最大限速设置 (包含子项 【限速】)
      local.max_speed_limit_section: MapEntry(
        CellSectionType.security,
        SettingRowModel(
          title: local.max_speed_limit_section,
          rowType: CellRowType.switchTile,
          switchValue: false,
          subItems: [
            SettingRowModel(
              title: local.max_speed_limit,
              rowType: CellRowType.arrowWithValue,
              isMainRow: false,
            ),
          ].obs,
        ),
      ),
      // 最大速度警告
      local.max_speed_warning: MapEntry(
        CellSectionType.security,
        SettingRowModel(
            title: local.max_speed_warning,
            rowType: CellRowType.arrowWithValue),
      ),

      // MARK:【偏好】 分组 ---
      // 骑行模式
      local.riding_mode: MapEntry(
        CellSectionType.preference,
        SettingRowModel(
            title: local.riding_mode, rowType: CellRowType.arrowWithValue),
      ),
      // 骑行辅助 (包含【加速辅助, 刹车辅助】)
      local.ride_assist: MapEntry(
        CellSectionType.preference,
        SettingRowModel(
          title: local.ride_assist,
          rowType: CellRowType.switchTile,
          switchValue: false,
          subItems: [
            SettingRowModel(
                title: local.acc_assist,
                rowType: CellRowType.arrowWithValue,
                isMainRow: false),
            SettingRowModel(
                title: local.brake_assist,
                rowType: CellRowType.arrowWithValue,
                isMainRow: false),
          ].obs,
        ),
      ),
      // 踏板灵敏度
      local.pedal_sensitivity: MapEntry(
        CellSectionType.preference,
        SettingRowModel(
            title: local.pedal_sensitivity,
            rowType: CellRowType.arrowWithValue),
      ),
      // 平衡角度
      local.balance_angle: MapEntry(
        CellSectionType.preference,
        SettingRowModel(
            title: local.balance_angle, rowType: CellRowType.arrowWithValue),
      ),
      // 校准 (包含多个子项)
      local.calibration: MapEntry(
        CellSectionType.preference,
        SettingRowModel(
          title: local.calibration,
          rowType: CellRowType.onlyValue,
          value: "",
          subItems: [
            SettingRowModel(
                title: local.balance_calibration,
                rowType: CellRowType.onlyArrow,
                isMainRow: false),
            SettingRowModel(
                title: local.turning_calibration,
                rowType: CellRowType.onlyArrow,
                isMainRow: false),
            SettingRowModel(
                title: local.motor_self_adjustment,
                rowType: CellRowType.onlyArrow,
                isMainRow: false),
          ].obs,
        ),
      ),
      // 边坡角模式
      local.berm_angle_mode: MapEntry(
        CellSectionType.preference,
        SettingRowModel(
            title: local.berm_angle_mode, rowType: CellRowType.switchTile),
      ),
      // 防打滑
      local.spin_kill: MapEntry(
        CellSectionType.preference,
        SettingRowModel(
            title: local.spin_kill, rowType: CellRowType.switchTile),
      ),

      //  MARK: 【声音】 分组 ---
      // 声波
      local.sound_wave: MapEntry(
        CellSectionType.sound,
        SettingRowModel(
            title: local.sound_wave, rowType: CellRowType.switchTile),
      ),
      // 声音包
      local.sound_pack: MapEntry(
        CellSectionType.sound,
        SettingRowModel(
            title: local.sound_pack, rowType: CellRowType.onlyArrow),
      ),
      // 声音 (包含音量子项)
      local.sound: MapEntry(
        CellSectionType.sound,
        SettingRowModel(
          title: local.sound,
          rowType: CellRowType.switchTile,
          subItems: [
            SettingRowModel(
                title: local.volume,
                rowType: CellRowType.arrowWithValue,
                isMainRow: false),
          ].obs,
        ),
      ),

      //  MARK: 【灯光】 分组 ---
      // LED 灯
      local.led_light: MapEntry(
        CellSectionType.light,
        SettingRowModel(title: local.led_light, rowType: CellRowType.onlyArrow),
      ),

      // tbox 分组 ---
      // TBOX 充电
      local.tbox_charge: MapEntry(
        CellSectionType.tbox,
        SettingRowModel(
            title: local.tbox_charge, rowType: CellRowType.switchTile),
      ),
      // TBOX 重启
      local.tbox_reboot: MapEntry(
        CellSectionType.tbox,
        SettingRowModel(
            title: local.tbox_reboot, rowType: CellRowType.onlyArrow),
      ),

      //  MARK: 【其他】 分组 ---
      // 待机时间
      local.stand_by_time: MapEntry(
        CellSectionType.other,
        SettingRowModel(
            title: local.stand_by_time, rowType: CellRowType.arrowWithValue),
      ),
      // 运输模式
      local.transport_mode: MapEntry(
        CellSectionType.other,
        SettingRowModel(
            title: local.transport_mode, rowType: CellRowType.switchTile),
      ),
      // 空载检测
      local.no_load_detect: MapEntry(
        CellSectionType.other,
        SettingRowModel(
            title: local.no_load_detect, rowType: CellRowType.onlyArrow),
      ),
      // 恢复出厂设置
      local.factory_reset: MapEntry(
        CellSectionType.other,
        SettingRowModel(
            title: local.factory_reset, rowType: CellRowType.onlyArrow),
      ),
    };

    // 遍历输入标题并按 mapping 放入对应分组（O(n)）
    for (final title in rowTitle) {
      final entry = mapping[title];
      if (entry == null) {
        // 未知 title：忽略（与原实现保持一致）
        continue;
      }
      switch (entry.key) {
        case CellSectionType.none:
          noneItems.add(entry.value);
          break;
        case CellSectionType.dataAndStatus:
          dataAndStatusItems.add(entry.value);
          break;
        case CellSectionType.security:
          securityItems.add(entry.value);
          break;
        case CellSectionType.preference:
          preferenceItems.add(entry.value);
          break;
        case CellSectionType.sound:
          soundItems.add(entry.value);
          break;
        case CellSectionType.light:
          lightItems.add(entry.value);
          break;
        case CellSectionType.tbox:
          tboxItems.add(entry.value);
          break;
        case CellSectionType.other:
          otherItems.add(entry.value);
          break;
        case CellSectionType.inquiry:
          inquiryItems.add(entry.value);
          break;
        case CellSectionType.repair:
          repairItems.add(entry.value);
          break;
        default:
          // 不应到达这里
          break;
      }
    }

    return [
      if (noneItems.isNotEmpty)
        SettingSectionModel(
            sectionType: CellSectionType.none, subItems: noneItems),
      if (dataAndStatusItems.isNotEmpty)
        SettingSectionModel(
            sectionType: CellSectionType.dataAndStatus,
            subItems: dataAndStatusItems),
      if (securityItems.isNotEmpty)
        SettingSectionModel(
            sectionType: CellSectionType.security, subItems: securityItems),
      if (preferenceItems.isNotEmpty)
        SettingSectionModel(
            sectionType: CellSectionType.preference, subItems: preferenceItems),
      if (soundItems.isNotEmpty)
        SettingSectionModel(
            sectionType: CellSectionType.sound, subItems: soundItems),
      if (lightItems.isNotEmpty)
        SettingSectionModel(
            sectionType: CellSectionType.light, subItems: lightItems),
      if (tboxItems.isNotEmpty)
        SettingSectionModel(
            sectionType: CellSectionType.tbox, subItems: tboxItems),
      if (otherItems.isNotEmpty)
        SettingSectionModel(
            sectionType: CellSectionType.other, subItems: otherItems),
      if (inquiryItems.isNotEmpty)
        SettingSectionModel(
            sectionType: CellSectionType.inquiry, subItems: inquiryItems),
      if (repairItems.isNotEmpty)
        SettingSectionModel(
            sectionType: CellSectionType.repair, subItems: repairItems),
    ];
  }

  // MARK: - 加透明度的箭头
  Widget _arrowWithOpacity() {
    return const Opacity(
      opacity: 0.3,
      child: Icon(Icons.chevron_right, color: Colors.white),
    );
  }
}
